/**
 * 
 */
package com.wbcs.jbsf.auth.abs.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.wbcs.config.component.application.report.ReportBean;
import com.wbcs.jbsf.auth.abs.AbstandLogin;
import com.wbcs.jbsf.util.Consts;
import com.wbcs.jbsf.util.Wbcs4JBSFUtil;
import com.wbcs.jbsf.util.WbcsUtils;
import com.wbcs.system.ReportRequest;
import com.wbcs.system.assistant.ReportAssistant;

/**
 * @author hawkfly
 *
 */
public class WbcsLogin extends AbstandLogin
{
    private Log log = LogFactory.getLog(WbcsLogin.class);
    private ReportRequest rrequest;
    private ReportBean rbean;
    private Object typeObj;
    private Object dataObj;
    /**
     * @param rq HttpServletRequest
     * @param rp HttpServletResponse
     */
    public WbcsLogin(ServletRequest rq, ServletResponse rp)
    {
        super(rq,rp);
        // TODO Auto-generated constructor stub
    }

    /* (non-Javadoc)
     * @see com.wbcs.jbsf.auth.abs.AbstandLogin#checklogin()
     */
    @Override
	public String checklogin()
    {
        if(rrequest == null || rbean == null ||dataObj == null){
            String errormsg = "参数未进行完全初始化！";
            log.error(errormsg);
            return errormsg;
        }
        List lstData = (List)dataObj;
        String errorup = "用户名或密码错误！";
        if(lstData.size() == 0)return errorup;
        Object rowDataObj = lstData.get(0);
        Object userid = ReportAssistant.getInstance().getPropertyValue(rowDataObj,"userid");
        Object username = ReportAssistant.getInstance().getPropertyValue(rowDataObj,"username");
        Object redirecturl = ReportAssistant.getInstance().getPropertyValue(rowDataObj,"redirecturl");
        Object sitename = ReportAssistant.getInstance().getPropertyValue(rowDataObj,"sitename");
        Object lmids = ReportAssistant.getInstance().getPropertyValue(rowDataObj,"lmids");//lmids
        Object orgid = ReportAssistant.getInstance().getPropertyValue(rowDataObj,"orgid");
        
        List<String> lstsiteid = new ArrayList<String>();
        for(Object item : lstData){
            Object siteid = ReportAssistant.getInstance().getPropertyValue(item,"siteid");
            lstsiteid.add(siteid == null ? null : String.valueOf(siteid));
        }
        String siteid = null;
        if(lstsiteid.size() > 0)siteid = lstsiteid.get(0);
        Object realname = ReportAssistant.getInstance().getPropertyValue(rowDataObj,"realname");
        if(redirecturl == null)return "用户名或密码错误！";
        if(username != null){
            request.getSession().setAttribute(Consts.SESSION_USERID,userid);
            request.getSession().setAttribute(Consts.SESSION_USERCODE,username);
            request.getSession().setAttribute(Consts.SESSION_SITEID,siteid);
            request.getSession().setAttribute(Consts.SESSION_SITENAME,sitename);
            request.getSession().setAttribute(Consts.SESSION_USERNAME,realname);
            request.getSession().setAttribute(Consts.SESSION_ORGID, orgid);
            
            request.getSession().setAttribute(Consts.SESSION_YEAR, WbcsUtils.getYear());
            request.getSession().setAttribute(Consts.SESSION_MONTH, WbcsUtils.getMonth());
            
            request.getSession().setAttribute(Consts.SESSION_LOGINTIME, WbcsUtils.getDefaultTime());
            //栏目不为空的情况下将栏目数据放入会话中
            if(!Wbcs4JBSFUtil.isNull(String.valueOf(lmids)))
            {
                request.getSession().setAttribute(Consts.SESSION_LMIDS,lmids);
            }
            
            setSendredirecturl(String.valueOf(redirecturl));
        }else{
            return errorup;
        }
        
        return CONTINUE;
    }

    public WbcsLogin initParams(ReportRequest rrequest,ReportBean rbean,Object typeObj,Object dataObj){
        this.rrequest = rrequest;
        this.rbean = rbean;
        this.typeObj = typeObj;
        this.dataObj = dataObj;
        setWbcsresponse(rrequest.getWResponse());
        return this;
    }
}

